package com.hoangkien.DAO;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CreateDatabase {
	public static final String TAG = "DBAdapter";
	private SQLiteDatabase sqliteDb;

	private SQLiteOpenHelper dbHelper;

	private static final String DATABASE_NAME = "SoundCloudMusic";
	private static final int DATABASE_VERSION = 1;
	
	public CreateDatabase(Context ctx) {
		this.dbHelper = new DBAdapterHelper(ctx);
	}

	public static CreateDatabase create(Context ctx) {
		return new CreateDatabase(ctx);
	}
	
	public synchronized SQLiteDatabase open() {
		if (sqliteDb == null || !sqliteDb.isOpen()) {
			try {
				sqliteDb = dbHelper.getWritableDatabase();
			} catch (Exception e) {
				Log.e(TAG, e.toString());
				e.printStackTrace();
			}
		}
		return sqliteDb;
	}

	public void close() {
		if (sqliteDb != null && sqliteDb.isOpen()) {
			sqliteDb.close();
		}

	}
	
	private static class DBAdapterHelper extends SQLiteOpenHelper {

		public DBAdapterHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			UserScheme.onCreate(db);
			SoundCloudScheme.onCreate(db);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		}

	}
}
